<?php
	session_start();	
?>

<?php
	if( $_SESSION["admin"] != "" )
	{
?>
<a href="index.php?com=productimage"><b>List product image</b></a> | <a href="index.php?com=productimage&func=add"><b>Add product image</b></a>
<?php
	include_once("process/productimage.php");	
	$productimage = new productimage();
	
	if( $_GET["com"] == "productimage" && $_GET["func"] == "update" && isset($_GET["id"]))
	{
		$row = $productimage->GetOneProductImage($_GET['id']);	
	}elseif( isset($_POST['btnUpdate']) )
	{	
        $file = $_FILES["ProductImage"];
		$file_name = $file["name"];
		$file_ext = explode(".",$file_name);
		if($file["name"] == "")
		{	
			$ProductNo = stripcslashes($_POST["ProductNo"]);
            $Picture=$file_name;
              $id = stripcslashes($_POST["ProductimageID"]);
        //echo $code, $name, $order;
            	$productimage->UpdateProductImage($ProductNo, $Picture, $id);	
        		echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=productimage" />';
        		return;
            
        }
        if($file_ext[count($file_ext)-1] == "jpg" || $file_ext[count($file_ext)-1] == "jpeg" || $file_ext[count($file_ext)-1] == "gif")
		{
		      $ProductNo = stripcslashes($_POST["ProductNo"]);
            $Picture=$file_name;
              $id = stripcslashes($_POST["ProductimageID"]);
        //echo $code, $name, $order;
            
        		$productimage->UpdateProductImage($ProductNo, $Picture, $id);
                move_uploaded_file($_FILES["Picture"]["tmp_name"],"asset/".$_FILES["Picture"]["name"]);	
        		echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=productimage" />';
        		return;
            
		}
        else
		{
			$alert='<script type="text/javascript">
			alert("Bạn chỉ được phép chọn file có định dạng jpg, jpeg, gif");
			</script>';
			echo $alert;
		}
	}
	else
	{
		echo '<meta http-equiv="Refresh" content="0;URL=index.php?com=productimage" />';
	}	
	
?>
<form action="index.php?com=productimage&func=update" method="post" id="contactform" enctype="multipart/form-data">
	<table width="630px" style="font-weight:bold; font-size:10pt;" border="0">
		<tr>
			<td align="center" colspan="4" height="50"><b style="font-size:12pt; color:Blue;">Update productimage</b></td>
		</tr>
		<tr height="26">
			<td align="right" width="120px">ProductNo :&nbsp;&nbsp;
				<input type="hidden" name="ProductimageID" id="ProductimageID" value="<?php echo $row['ProductimageID'] ?>" />
			</td>
			<td width="140px"><input type="text" class="validate[required]" name="ProductNo" id="ProductNo" value="<?php echo $row['ProductNo']; ?>" style="width: 120px;" /><font style="color: red;"> *</font>
				<input type="hidden" name="ProductimageID" id="ProductimageID" value="<?php echo $row['ProductimageID'] ?>" />
			</td>
        </tr>
         <tr>
			<td align="right" width="120px">Image :&nbsp;&nbsp;</td>
			<td><input type="file" name="ProductImage" id="ProductImage"/><font style="color: red;"> *</font></td>
		</tr>  	    
		<tr>
			<td></td>
			<td colspan="3" height="40">
				<input type="submit" name="btnUpdate" id="btnUpdate" value="Update" />
				<input type="reset" value="Reset" />
			</td>
		</tr>
    </table>
</form><br><br>
<i>Please fill in these fields (<font style="color: red;">*</font>)</i>
<?php
    }
   else
	{
		echo "<meta http-equiv='refresh' content='0; url=login.php' />";
	}
?>